package com.system.controller;

import com.system.pojo.Contract;
import com.system.pojo.SysUser;
import com.system.service.ContractService;
import com.system.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


@Controller
@ResponseBody
@RequestMapping("/contract")
public class ContractController {
	
	@Autowired
	private ContractService contractService;


	/**
	 * 合同：客户名，销售人员名
	 * 产品：产品名称，数量，单价
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/insert")
	public ResultVo<Contract> insert(@RequestBody Contract contract) {
		ResultVo<Contract> resultVo = new ResultVo<>();
		try {
			resultVo = contractService.insert(contract);
		}catch (Exception e){
			e.printStackTrace();
			resultVo.setMessage("添加合同失败");
		}finally {
			return resultVo;
		}
	}

	/*
	只更新合同的客户，销售人员信息，
	不更新合同商品表
	 */
	@RequestMapping("/update")
	public ResultVo<Contract> update(@RequestBody Contract contract) {
		ResultVo<Contract> resultVo = new ResultVo<>();
		try {
			resultVo = contractService.updateById(contract);
		}catch (Exception e){
			e.printStackTrace();
			resultVo.setMessage("更新合同失败");
		}finally {
			return resultVo;
		}
	}

	/**
	 * 显示合同详细信息
	 * @param contractId
	 * @return
	 */
	@RequestMapping("/selectDetail/{contractId}")
	public ResultVo<Contract> selectDetail(@PathVariable Integer contractId){
		ResultVo<Contract> resultVo = new ResultVo<>();
		try {
			resultVo = contractService.selectDetail(contractId);
		}catch (Exception e){
			e.printStackTrace();
			resultVo.setMessage("查询合同失败");
		}finally {
			return resultVo;
		}
	}


	/**
	 * 查询所有已完成合同
	 * @return
	 */
	@RequestMapping("/selectAllDone")
	public ResultVo<Contract> selectAllDone() {
		ResultVo<Contract> resultVo = new ResultVo<>();
		try {
			resultVo = contractService.selectAllDone();
		}catch (Exception e){
			e.printStackTrace();
			resultVo.setMessage("查询失败");
		}finally {
			return resultVo;
		}
	}

	/**
	 * 查询所有未完成合同
	 * @return
	 */
	@RequestMapping("/selectAllUndone")
	public ResultVo<Contract> selectAllUndone() {
		ResultVo<Contract> resultVo = new ResultVo<>();
		try {
			resultVo = contractService.selectAllUndone();
		}catch (Exception e){
			e.printStackTrace();
			resultVo.setMessage("查询失败");
		}finally {
			return resultVo;
		}
	}



	@RequestMapping("/saler")
	public ResultVo<Contract> salerContracts(HttpSession session){
		ResultVo<Contract> resultVo = new ResultVo<Contract>();
		SysUser sysUser =(SysUser) session.getAttribute("sysUser");
		try {
			if (sysUser == null){
				resultVo.setMessage("查询合同失败");
			}else{
				Integer uid = sysUser.getId();
				resultVo = contractService.salerContracts(uid);
			}
		}catch (Exception e){
			e.printStackTrace();
			resultVo.setMessage("查询失败");
		}finally {
			return resultVo;
		}
	}


	@RequestMapping("/addproduct")
	public ResultVo<Contract> addproduct(@RequestBody Contract contract){
		ResultVo<Contract> resultVo = new ResultVo<Contract>();
		try {
			resultVo = contractService.addproduct(contract);
		}catch (Exception e){
			e.printStackTrace();
			resultVo.setMessage("添加产品失败");
		}finally {
			return resultVo;
		}
	}
}
